/*
* @Author: wangliang
* @Date: 2018/5/16
* @description: 运营标记
*/
<template>
    <el-dropdown trigger="click" class="m-l-15" @command="chooseOperate">
        <el-button type="primary" plain>{{operateStatus}}<i class="el-icon-arrow-down el-icon--right"></i></el-button>
        <el-dropdown-menu slot="dropdown">
            <el-dropdown-item 
            v-for="(item, i) in operateStatusList"
            :key="i"
            :command="item.dictName"
            >{{item.dictName}}</el-dropdown-item>
        </el-dropdown-menu>
    </el-dropdown>
</template>
<script lang="ts">
import Vue from 'vue';
import { Component, Watch, Prop } from 'vue-property-decorator';
import BasicInfoService from '../../../../../service/subject/company/basic-info/index';
import ProductService from '../../../../../service/subject/company/other-info/index';
@Component({
  components: {}
})
export default class OperateInfo extends Vue {
    @Prop({ default: '' })
    public operateType: string;

    public operateStatus: string = '运营标记';
    public basicOperate: number = 2; //运营标记
    public operateStatusList: object[] = [
        {
            dictValue: 1,
            dictName: '运营'
        },
        {
            dictValue: 2,
            dictName: '不运营'
        },
    ];
    // 声明周期钩子
    public mounted() {
        this.getOperate();
        this.$emit('getOperateNum', this.basicOperate);
    }
    // watch
    @Watch('msg')
    public onChildChanged(val: string, oldVal: string) {
        return val + '***';
    }
    // 更改运营标记
    public chooseOperate(command: string) {
        this.operateStatus = command;
        if (command === '运营') {
            this.basicOperate = 1;
            this.operateStatusTrue();
        } else if (command === '不运营') {
            this.basicOperate = 2;
        }
        this.$emit('getOperateNum', this.basicOperate);
        this.changeOperate(this.basicOperate);
    }
    // 获取运营标记
    public async getOperate() {
        const res = await BasicInfoService.getCompanyById(Number(this.$route.params.itemId));
        if (res.resCode === '0') {
            if (this.operateType === 'operativeProducts') { // 产品服务
                this.basicOperate = res.data.productOperate;
            }
            if (this.operateType === 'operativeBusiness') { // 企业业务
                this.basicOperate = res.data.businessOperate;
            }
            if (this.operateType === 'operativeCompetitors') { // 竞品信息
                this.basicOperate = res.data.competitorOperate;
            }
            if (this.basicOperate === 2) {
                this.operateStatus = '不运营';
            } else if (this.basicOperate === 1) {
                this.operateStatus = '运营';
            }
        }
    }
    // 更改基本信息-运营
    public async changeOperate(operate: number) {
        const res = await BasicInfoService.getCompanyById(Number(this.$route.params.itemId));
        if (res.resCode === '0') {
            const basicInfoOperate = res.data;
            if (this.operateType === 'operativeProducts') { // 产品服务
                basicInfoOperate.productOperate = operate;
            }
            if (this.operateType === 'operativeBusiness') { // 企业业务
                basicInfoOperate.businessOperate = operate;
                this.basicOperate = res.data.businessOperate;
            }
            if (this.operateType === 'operativeCompetitors') { // 竞品信息
                basicInfoOperate.competitorOperate = operate;
            }
            await BasicInfoService.updateCompany(basicInfoOperate);
        }
    }
    // 运营状态
    public async operateStatusTrue() {
        if (this.operateType === 'operativeProducts') { // 产品服务
            await ProductService.operativeProducts(Number(this.$route.params.itemId));
        }
        if (this.operateType === 'operativeBusiness') { // 企业业务
            await ProductService.operativeBusiness(Number(this.$route.params.itemId));
        }
        if (this.operateType === 'operativeCompetitors') { // 竞品信息
            await ProductService.operativeCompetitors(Number(this.$route.params.itemId));
        }
    }
}
</script>